﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using Delay;
using System.IO.IsolatedStorage;
using System.ComponentModel;
using System.Threading;
using System.Windows.Resources;
using System.Windows.Media.Imaging;

namespace PhoneInfo.Page
{
    public partial class mian : PhoneApplicationPage
    {
        private BackgroundWorker bkgWorkPk = null;
        public mian()
        {
                InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //清空历史记录防止Back键回退
            bkgWorkPk = new BackgroundWorker();
            JournalEntry entry = NavigationService.RemoveBackEntry();
            while (entry != null)
            {
                string pageName = entry.Source.ToString();
                entry = NavigationService.RemoveBackEntry();
            }
            if (App.appSitting.Theme != 1) imgBtn.Tag = "/Icon/black/model.png";
        }
        private void List_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if ((mainNavigationItem)((ListBox)sender).SelectedItem == null && ((ListBox)sender).SelectedIndex == -1)
                return;
            switch (((mainNavigationItem)((ListBox)sender).SelectedItem).Title)
            {
                case "手机搜索":
                    {
                        string pagename = "/Page/SeparatorCenter.xaml?action=search";
                        NavigationService.Navigate(new Uri(pagename, UriKind.Relative));
                    }
                    break;
                case "手机排行":
                    {
                        string pagename = "/Page/Most.xaml";
                        NavigationService.Navigate(new Uri(pagename, UriKind.Relative));
                        RunBkgWorker(bkgWorkPk, 3);
                    }
                    break;
                case "手机PK":
                    {
                        string pagename = "/Page/PK.xaml";
                        NavigationService.Navigate(new Uri(pagename, UriKind.Relative));
                    }
                    break;
                case "更多..":
                    {
                        string pagename = "/Page/MoreFromInfo.xaml";
                        NavigationService.Navigate(new Uri(pagename, UriKind.Relative));
                    }
                    break;
            }
        }
        private void ShowSelf_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null) return;
            if (App.selfResult.SearchResultItems == null || App.selfResult.SearchResultItems.Count == 0)
            {
                ToastPrompt toast = new ToastPrompt() { Message = "没有找到您的机型相关的信息，请重新登录后再试!" };
                toast.Show();
                return;
            }
            string objective = "/Page/TypeData.xaml";
            objective += "?pid=" + App.selfResult.SearchResultItems[0].Ponhe1_pid;
            objective += "&price=" + App.selfResult.SearchResultItems[0].Ponhe1_price;
            objective += "&maxprice=" + App.selfResult.SearchResultItems[0].Ponhe1_maxprice;
            objective += "&minprice=" + App.selfResult.SearchResultItems[0].Ponhe1_minprice;
            objective += "&picurl=" + App.selfResult.SearchResultItems[0].Ponhe1_picurl;
            objective += "&ppname=" + App.selfResult.SearchResultItems[0].Ponhe1_ppname;

            objective += "&phonename=" + App.selfResult.SearchResultItems[0].Ponhe1_phonename;
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            mainNavigation mainNavigation = new mainNavigation();
            mainNavigationItem item_1 = new mainNavigationItem
            {
                Title = "手机搜索",
                Picurl = App.appSitting.Theme == 1 ? "Icon/pic_02.jpg" : "Icon/black/pic_02.jpg"
            };
            mainNavigation.MainNavigationItems.Add(item_1);
            mainNavigationItem item_2 = new mainNavigationItem
            {
                Title = "手机排行",
                Picurl = App.appSitting.Theme == 1 ? "Icon/pic_03.jpg" : "Icon/black/pic_03.jpg"
            };
            mainNavigation.MainNavigationItems.Add(item_2);
            mainNavigationItem item_3 = new mainNavigationItem
            {
                Title = "手机PK",
                Picurl = App.appSitting.Theme == 1 ? "Icon/pic_04.jpg" : "Icon/black/pic_04.jpg"
            };
            mainNavigation.MainNavigationItems.Add(item_3);
            mainNavigationItem item_4 = new mainNavigationItem
            {
                Title = "更多..",
                Picurl = App.appSitting.Theme == 1 ? "Icon/pic_05.jpg" : "Icon/black/pic_05.jpg"
            };
            mainNavigation.MainNavigationItems.Add(item_4);
            this.ContentPanel.DataContext = mainNavigation;
        }
        private void RunBkgWorker(BackgroundWorker bkgWork, int type)
        {
            bkgWork.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                Thread.Sleep(50);
            };
            bkgWork.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                switch (type)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                        this.phonePkTemp.IsOpen = false;
                        break;
                }
            };
            bkgWork.RunWorkerAsync();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string pagename = "/Page/MobileProduct.xaml";
            NavigationService.Navigate(new Uri(pagename, UriKind.Relative));
        }
    }
}